package com.hc.accountservice.mapper;

import com.hc.accountservice.sqlprovider.SelectPayRecordSql;
import com.hc.entity.PayRecord;
import com.hc.entity.Wallet;
import com.hc.entity.WalletOpr;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * @BelongsProject: hc
 * @BelongsPackage: com.hc.accountservice.mapper
 * @Author: WY
 * @CreateTime: 2022-10-19  16:14
 * @Descriprion: 账户mapper
 * @Version: 1.0
 **/
public interface AccountMapper {

    /**
     *条件查询支付记录
     * @param payRecord
     * @return
     */
    @SelectProvider(type = SelectPayRecordSql.class,method = "select")
    @Results(
            {
                    @Result(column = "id",property = "id",id = true),
                    @Result(column = "datetime",property = "adateTime"),
                    @Result(column = "way",property = "way"),
                    @Result(column = "order_id",property = "order",one = @One(select = "com.hc.accountservice.mapper.OrderMapper.findById",fetchType = FetchType.LAZY))
            }
    )
    List<PayRecord> findByPage(PayRecord payRecord);

    /**
     * 查询单条订单信息
     * @param id
     * @return
     */
    @Select("select p.*,o.id oid from hc_payrecord p,hc_order o " +
            "where p.id=#{id} and p.order_id=o.id")
    @Results(
            {
                    @Result(column = "id",property = "id",id = true),
                    @Result(column = "datetime",property = "adateTime"),
                    @Result(column = "fee",property = "fee"),
                    @Result(column = "way",property = "way"),
                    @Result(column = "oid",property = "order.id"),
                    @Result(column = "user_id",property = "userId")
            }
    )
    PayRecord findById(int id);

    /**
     * 订单退款后删除支付记录
     * @param id
     */
    @Delete("delete from hc_payrecord where id=#{id}")
    void del(int id);

    /**
     * 订单退款后修改订单状态为已退款
     * @param id
     */
    @Update("update hc_order set status='t' where id=#{id}")
    void updatestatus(int id);

    /**
     * 余额查询
     * @param userid
     * @return
     */
    @Select("select *from hc_wallet where user_id=#{userid}")
    @Results(
            {
                    @Result(column = "id",property = "id",id = true),
                    @Result(column = "balance",property = "balance"),
                    @Result(column = "user_id",property = "userId")
            }
    )
    Wallet findBalance(int userid);

    /**
     * 钱包充值
     * @param wallet
     */
    @Update("update hc_wallet set balance=#{balance} where user_id=#{userId}")
    void topUp(Wallet wallet);

    /**
     * 钱包操作记录
     * @param walletOpr
     */
    @Insert("insert into hc_walletopr values(null,#{dateTime},#{way},#{orderId},#{status},#{walletId})")
    void moneyLog(WalletOpr walletOpr);

}
